package cn.shyouchi.wuguanjia.android.ui;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.controls.BasicEditDialogFragment;

public class MultiChoicesActivity extends AppCompatActivity implements
        BasicEditDialogFragment.BasicEditDialogListener {

    @BindView(R.id.multi_choice_container)
    GridLayout container;
    private static final String TAG = "MultiChoices";
    private String editDialogTitle, editDialogHint;
    private float dp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_choices);
        ButterKnife.bind(this);
        dp = getResources().getDisplayMetrics().density;
        Log.d(TAG, "" + getResources().getDisplayMetrics().density);
        Bundle b = getIntent().getExtras();
        String title = b.getString("title");
        String[] choicesArray = b.getStringArray("choices");
        editDialogTitle = b.getString("edit_dialog_title");
        editDialogHint = b.getString("edit_dialog_hint");
        setTitle(title);
        for (String choice : choicesArray) {
            generateChoice(choice);
        }

    }

    @OnClick(R.id.multi_choice_add)
    void addTapped() {
        BasicEditDialogFragment dialog = BasicEditDialogFragment.newInstance(
                editDialogTitle, editDialogHint, InputType.TYPE_CLASS_TEXT, false);
        dialog.setEnterClickListener(this);
        dialog.show(getFragmentManager(), "EditDialogFragment");
    }

    @OnClick(R.id.multi_choice_save)
    void saveTapped() {
        ArrayList<String> choiceList = new ArrayList<>();
        for (int i = 0; i < container.getChildCount(); i++) {
            TextView child = (TextView) container.getChildAt(i);
            if (child.getCurrentTextColor() == Color.WHITE) {
                choiceList.add(child.getText().toString());
            }
        }
        for (String choice : choiceList) {
            Log.d(TAG, choice);
        }
        if (choiceList.size() > 3) {
            Toast.makeText(this, "最多可以选择三个", Toast.LENGTH_SHORT).show();
        } else if (choiceList.size() == 0) {
            Toast.makeText(this, "请至少选择一个", Toast.LENGTH_SHORT).show();
        } else {
            JSONArray jsonArray = new JSONArray(choiceList);
            Log.d(TAG, jsonArray.toString());
            Intent resultIntent = new Intent();
            resultIntent.putExtra("CHOICES", choiceList);
            setResult(Activity.RESULT_OK, resultIntent);
            finish();
        }
    }

    private void generateChoice(String choice) {
        final TextView textView = new TextView(this);
        textView.setBackgroundColor(Color.LTGRAY);
        textView.setText(choice);
        textView.setGravity(Gravity.CENTER);
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = (int) (88 * dp);
        params.height = (int) (45 * dp);
        params.leftMargin = (int) (10 * dp);
        params.topMargin = (int) (10 * dp);
        textView.setLayoutParams(params);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (textView.getCurrentTextColor() != Color.WHITE) {
                    textView.setBackgroundColor(ContextCompat.getColor(MultiChoicesActivity.this, R.color.mainColor));
                    textView.setTextColor(Color.WHITE);
                } else {
                    textView.setBackgroundColor(Color.LTGRAY);
                    textView.setTextColor(Color.DKGRAY);
                }

            }
        });

        container.addView(textView, container.getChildCount() - 1);
    }

    @Override
    public void onBasicEditDialogPositiveClick(DialogFragment dialog, String content) {
        generateChoice(content);
    }
}
